package com.ordertracker.service;

import com.ordertracker.core.PersistentEventRegister;
import com.ordertracker.dao.GeneralCrudDao;
import com.ordertracker.listener.EntityConstraintSupport;
import com.ordertracker.listener.GeneralEntityListener;
import com.ordertracker.listener.UserEntityConstraint;
import com.ordertracker.persistent.constant.ROLE_TYPE;
import com.ordertracker.persistent.constant.SEX;
import com.ordertracker.persistent.dictionary.*;
import com.ordertracker.util.DateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * Description of class.
 * Created 1/8/13 1:40 PM
 *
 * @author andrey.rodin@playtech.com
 */

@Component
public class InitialService {


    private GeneralCrudDao dao;

    @Resource(name = "crudDao")
    public void setDao(GeneralCrudDao dao) {
        this.dao = dao;
    }

    public void checkUsers() {
        if (dao.getAll(User.class).size() == 0) {
            createAdminUser();
            createPositions();
            createOrganization();
            createEmployee();
            createClient();
            createClientUser();
            createUserOrganization();
        }
    }

    public void createPositions() {
        dao.saveEntity(new Position("Director"));
        dao.saveEntity(new Position("Manager"));
        dao.saveEntity(new Position("Seller"));
    }

    public void createAdminUser(){
        User adminUser = new User();
        Person person = new Person();
        person.setFirstName("root");
        person.setLastName("root");
        person.setContactPhone("0000000");
        person.setGender(SEX.MALE);
        person.setOld((byte) 30);
        Person prs = (Person) dao.saveEntity(person);
        adminUser.setPerson(prs);



        Role role = (Role) dao.saveEntity(new Role("super admin", ROLE_TYPE.ADMIN));
        adminUser.setRole(role);
        adminUser.setLogin("root");
        adminUser.setPassword("root");
        adminUser.setDescription("super admin");
        adminUser.setEmail("admin@ordertracker.com");
        adminUser.setName("admin");
        adminUser.setTimezone("UTC+02:00");
        adminUser.setLocale("ru_RU");
        dao.saveEntity(adminUser);
    }

    public void createOrganization() {
        HashMap<String, String> authMap = new HashMap<String, String>(2);
        authMap.put("login", "root");
        final User user = (User) dao.findEqualByCriteria(authMap, User.class);
        AuditEntity auditEntity = new AuditEntity();
        Date currDate = DateUtils.getCurrDate();
        auditEntity.setCreatedDate(currDate);
        auditEntity.setCreatedByUser(user);
        auditEntity.setLastUpdateByUser(user);
        auditEntity.setLastUpdate(currDate);
        Organization organization = new Organization();
        organization.setAuditField(auditEntity);
        organization.setFullName("Default organization");
        organization.setPrefix("DF");
        organization.setShortName("Default");
        organization.setDescription("Auto generate");
        dao.saveEntity(organization);
    }

    public void createEmployee() {
        HashMap<String, String> authMap = new HashMap<String, String>(1);
        authMap.put("login", "root");
        final User user = (User) dao.findEqualByCriteria(authMap, User.class);
        authMap.clear();
        authMap.put("prefix", "DF");
        final Organization org = (Organization) dao.findEqualByCriteria(authMap, Organization.class);
        authMap.clear();
        authMap.put("name", "Manager");
        Position position = (Position) dao.findEqualByCriteria(authMap, Position.class);
        Employee employee = new Employee(user.getRole(),user, org, position);
        employee.setCode(0);
        employee.setName("default");
        dao.saveEntity(employee);
    }

    public void createClient() {
        HashMap<String, String> authMap = new HashMap<String, String>(1);
        authMap.put("prefix", "DF");
        final Organization org = (Organization) dao.findEqualByCriteria(authMap, Organization.class);
        Client client = new Client("Default", org);
        client.setCode("XXXXXX");
        client.setDescription("Auto generate");
        dao.saveEntity(client);
    }

    public void createClientUser() {
        User adminUser = new User();
        Person person = new Person();
        person.setFirstName("Ivanov");
        person.setLastName("Ivan");
        person.setContactPhone("0000000");
        person.setGender(SEX.MALE);
        person.setOld((byte) 35);
        Person prs = (Person) dao.saveEntity(person);
        adminUser.setPerson(prs);

        Role role = (Role) dao.saveEntity(new Role("client", ROLE_TYPE.CLIENT));
        adminUser.setRole(role);
        adminUser.setLogin("ivanov");
        adminUser.setPassword("ivanov");
        adminUser.setDescription("client");
        adminUser.setEmail("ivanov@ordertracker.com");
        adminUser.setName("ivanov");
        adminUser.setLocale("ru_RU");
        adminUser.setTimezone("UTC+02:00");
        User usr = (User) dao.saveEntity(adminUser);

        ClientEmployee clientEmployee = new ClientEmployee(usr, (Client)dao.findEqualByCriteria("name","Default",Client.class));
        dao.saveEntity(clientEmployee);
    }

    public void createUserOrganization() {
        Organization organization = (Organization) dao.findEqualByCriteria("prefix", "DF", Organization.class);
        Position position = (Position) dao.findEqualByCriteria("name","Manager", Position.class);
        User adminUser = new User();
        Person person = new Person();
        person.setFirstName("Petrov");
        person.setLastName("Petr");
        person.setContactPhone("0000000");
        person.setGender(SEX.MALE);
        person.setOld((byte) 25);
        Person prs = (Person) dao.saveEntity(person);
        adminUser.setPerson(prs);

        Role role = (Role) dao.saveEntity(new Role("manager", ROLE_TYPE.MANAGER));
        adminUser.setRole(role);
        adminUser.setLogin("petrov");
        adminUser.setPassword("petrov");
        adminUser.setDescription("organization manager");
        adminUser.setEmail("petrov@ordertracker.com");
        adminUser.setName("petrov");
        adminUser.setLocale("ru_RU");
        adminUser.setTimezone("UTC+02:00");
        User usr = (User) dao.saveEntity(adminUser);

        Employee employee = new Employee(role, usr, organization, position);
        employee.setName(adminUser.getName());
        dao.saveEntity(employee);

    }

    public void registerConstraints(PersistentEventRegister constraintsRegister) {
        EntityConstraintSupport userListener = new UserEntityConstraint(constraintsRegister);
        EntityConstraintSupport generalListener = new GeneralEntityListener(constraintsRegister);
        generalListener.registerListener();
        userListener.registerListener();
    }
}
